package org.test.rpc;

import java.io.IOException;

import org.apache.hadoop.ipc.ProtocolSignature;
import org.test.rpc.proto.EchoUserProtocol.UserReqProto;
import org.test.rpc.proto.EchoUserProtocol.UserRespProto;

import com.google.protobuf.RpcController;
import com.google.protobuf.ServiceException;

public class UserPBService
		implements
		UserPB {
	private UserBusiness impl;
	public UserPBService(UserBusiness impl){
		this.impl = impl;
	}
	
	
	
	public UserRespProto getUser(RpcController controller, UserReqProto request)
			throws ServiceException {
		User user = new User();
		user.setId(request.getId());
		user.setUserName(request.getUserName());
		UserResp respUser = impl.getUser(user);
		return UserRespProto.newBuilder().setSex(respUser.getSex()).setAddress(respUser.getAddress()).build();
	}

	public long getProtocolVersion(String protocol, long clientVersion)
			throws IOException {
		return versionID;
	}

	public ProtocolSignature getProtocolSignature(String protocol,
			long clientVersion, int clientMethodsHash) throws IOException {
		if (protocol.equals(UserPB.class.getName())){
	        return new ProtocolSignature(versionID, null);
		}
	    throw new IOException("Unknown protocol: " + protocol);
	}

	

}
